<?php
/*
 *      Patrick Bores - WebUserPrefs - SpamAssassin user_prefs file editor 
 *      Copyright (C) 2003  Patrick Bores
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version 2
 *      of the License, or (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
function mesa_head($bordercolor,$cellpadding,$cellspacing,$width){
	echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"$width\">\n";
	echo "<tr><td bgcolor=\"$bordercolor\">\n";
	echo "<table border=\"0\" cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" width=\"100%\">\n";
}
function mesa_tail(){
	echo "</table></td></tr></table>";
}
function sync_user_prefs(){
	global $user_prefs;
	global $user_prefs_array;
	global $prefs_source;
	global $db_sauser;
	global $db_table;
	if($prefs_source == "file") {
		if(!($fileptr = fopen("$user_prefs","w"))){
			return false;
		}
		foreach($user_prefs_array as $data){
			fputs($fileptr,$data);
		}
		fclose($fileptr);
	} else if($prefs_source == "db") {
		foreach($user_prefs_array as $data){
			preg_match("/^([\S]+)[\s]+(.*)$/",$data,$matches);
			if($matches[1] == "whitelist_from" || $matches[1] == "whitelist_to" 
				|| $matches[1] == "blacklist_from" || $matches[1] == "whitelist_from_rcvd")
				continue;
			$res = mysql_query("SELECT COUNT(username) FROM $db_table WHERE preference='$matches[1]' AND username='$db_sauser'");
			$row = mysql_fetch_row($res);
			if($row[0] == 0){
				mysql_query("INSERT INTO $db_table(username,preference,value) VALUES('$db_sauser','$matches[1]','$matches[2]')");
			} else {
				mysql_query("UPDATE $db_table SET value='$matches[2]' WHERE username='$db_sauser' AND preference='$matches[1]'");
			}
		}
	}
	return true;
}
function get_pref($pref_name){
	global $user_prefs_array;
	global $prefs_dir_array;
	if(is_array($prefs_dir_array) && array_key_exists($pref_name,$prefs_dir_array)){
		$data = $user_prefs_array[$prefs_dir_array[$pref_name]];
		preg_match("/^([\S]+)[\s]+(.*)$/",$data,$matches);
		return $matches[2];
	}else{
		return "";
	}
}
function set_pref($pref_name,$pref_value){
	global $prefs_change_array;
	$prefs_change_array[$pref_name] = $pref_value;
}
?>
